<?php
namespace App\Console\Commands;
use App\Common\Logs\Logs;
use App\Models\WebsitesTopic;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;

class TaskAdd extends Command
{
    /**
     * CreateArticle {param1?} {param2?}
     * The name and signature of the console command.
     * @var string
     */
    protected $signature = 'TaskAdd';


    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '栏目每天生成5篇文章';

    /**
     * Execute the console command.
     */
    public function handle()
    {
        $planTime  = date('Y-m-d 00:10:00', strtotime('+1 day'));
        $allowData = [];
        $websiteTask = DB::table('pd_website_crontab')->where("status",1)->get()->toArray();
        if($websiteTask){
            foreach ($websiteTask as $v){
                $allowData[]=$v->website_id;
            }
        }
        if(!$allowData){
            Logs::crontabLog("暂无网站脚本任务",[],"task_add");
            return false;
        }
        $query = WebsitesTopic::query();
        $data = $query
            ->whereIn("website_id",$allowData)
            ->where("is_del",1)
            ->where('status',1)
            ->select("id",'website_id')
            ->get()
            ->toArray();
        if($data){
            $postData =[];
            foreach ($data as $key=>$val){
                $postData[$key]['websites_id'] = $val['website_id'];
                $postData[$key]['websites_topic_id'] = $val['id'];
                $postData[$key]['plan_time'] = strtotime($planTime);
                $postData[$key]['plan_numbers'] = 1;
                $postData[$key]['admin_id'] = 1;
                $postData[$key]['operation_id'] = 1;
                $postData[$key]['created_at'] = date("Y-m-d H:i:s");
            }
            $res = DB::table("pd_tasks")->insert($postData);
            if(!$res){
                Logs::crontabLog("添加失败",$postData,"task_add");
            }
            Logs::crontabLog("添加成功",$postData,"task_add");
            echo "success:".date("Y-m-d H:i:s");
        }
    }


}
